package com.itic.mall.system.users.openstore.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.UserType;
import com.itic.appbase.framework.utils.RandomStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.inter.users.openstore.persistence.dao.OpenStoreDao;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStore;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStoreExample;
import com.itic.mall.system.inter.users.openstore.persistence.model.OpenStoreExample.Criteria;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMall;
import com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMallExample;
import com.itic.mall.system.users.usersmall.service.UsersMallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class OpenStoreService extends CrudService<OpenStoreDao,OpenStore,OpenStoreExample> {
	
	//默认入驻成功标识
	private static String ENTER_SUCCESS="171127"; 
	
    @Autowired
    private OpenStoreDao openStoreDao;
    @Autowired
    private UsersMallService usersMallService;

	public List<OpenStore> getByUserIdAndStatus(String userId, String status) {
		OpenStoreExample example = new OpenStoreExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andUserIdEqualTo(userId);
		criteria.andStatusEqualTo(status);
		List<OpenStore> list = openStoreDao.selectByExample(example);
		return list;
	}
	
	/**
	 * 根据用户id获取推广位
	 * @author 王文鑫
	 * @date 2018年2月24日 上午10:18:28
	 * @param userid
	 * @return
	 */
	public OpenStore getByUserId(String userid ){
		return openStoreDao.getByUserId(userid);
	}
	
	public OpenStore getById(String id ){
		return openStoreDao.getById(id);
	}


	public String editOpenStore(OpenStore openStore) {
		//开店成功
		UsersMall usersMall = usersMallService.get(openStore.getUserId());
		usersMall.setType(UserType.ydz.getType());
		usersMall.setInvitingCode( getYunInviteCode() );
	    usersMall.setParentId("0");//最高级云店主  友惠家ID
		usersMallService.addTeamCountByUsersMall(usersMall);
		return SUCCESS;
	}
	
	/**
     * 根据用户ID获取用户信息
     * @param userId
     * @return
     */
    public String getYunInviteCode(){
    	String randomNumStr = RandomStringHelper.randomNumStr(6);
    	UsersMall userByInviteCode = getUserByInviteCode(randomNumStr);
    	if( null != userByInviteCode){
    		return getYunInviteCode();
    	}else{
    		return randomNumStr;
    	}
    }
    
    /**
     * 根据用户ID获取用户信息
     * @param userId
     * @return
     */
    public UsersMall getUserByInviteCode(String inviteCode){
    	UsersMall result = null;
    	if(StringHelper.isNotBlank(inviteCode)){
    		UsersMallExample example = new UsersMallExample();
    		com.itic.mall.system.inter.users.usersmall.persistence.model.UsersMallExample.Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		criteria.andInvitingCodeEqualTo(inviteCode);
    		List<UsersMall> list = usersMallService.list(example);
    		if(list.size()>0){
    			result = list.get(0);
    		}
    	}
    	return result;
    }


    /**
     * 入驻成功的方法
     * @param openStore
     * @return
     */
	public String ruzhuSuccess(OpenStore openStore) {
		//编辑用户，判断有没有邀请码，如果有就添加下级，没有添加公司下级
	    openStoreDao.updateByPrimaryKey( openStore );
	    
	    String inviteCode = ENTER_SUCCESS;
	    
	    if( StringHelper.isNotBlank( openStore.getInviteCode()) ){
	    	inviteCode = openStore.getInviteCode();
	    }
	    
	    UsersMall parentUser = usersMallService.getUserByInviteCode( inviteCode );
    	
    	UsersMall user = usersMallService.get(openStore.getUserId());
    	user.setType(UserType.ydz.getType());
    	user.setYunId(parentUser.getId());
		user.setParentId(parentUser.getId());
		user.setInvitingCode( getYunInviteCode() );
		usersMallService.edit(user);
		
		usersMallService.addTeamCountByUsersMall(user);
		
		return SUCCESS;
	}


}